package com.chujian.framework.util;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：chujian
 * @since ：2020-12-01
 */
public class DateUs implements IDatePattern {

    private static final ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault();

    /**
     * 将localDateTime转为秒
     *
     * @param localDateTime
     * @return
     */
    public static Long toEpochSecond(LocalDateTime localDateTime) {
        if (localDateTime == null) return null;
        return localDateTime.atZone(DEFAULT_ZONE_ID).toEpochSecond();
    }

    /**
     * 将localDateTime转为毫秒
     *
     * @param localDateTime
     * @return
     */
    public static Long toEpochMilli(LocalDateTime localDateTime) {
        if (localDateTime == null) return null;
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     * 获取当前时间的秒
     *
     * @return
     */
    public static Long nowSecond() {
        return LocalDateTime.now().atZone(DEFAULT_ZONE_ID).toEpochSecond();
    }

    /**
     * 当前毫秒值
     *
     * @return
     */
    public static Long nowMilli() {
        return toEpochMilli(LocalDateTime.now());
    }

    public static LocalDateTime toLocalDateTime(long millis) {
        return new Date(millis).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime dayStrToLocalDateTime(String dayStr) {
        return toLocalDateTime(dayStr + " 00:00:00");
    }

    public static LocalDateTime dayToLocalDateTime(LocalDate day) {
        String dayStr = formatDay(day);
        return dayStrToLocalDateTime(dayStr);
    }

    public static LocalDateTime toLocalDateTime(String str) {
        if (StringUtils.isEmpty(str)) return null;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        return LocalDateTime.parse(str, dateTimeFormatter);
    }

    public static LocalDateTime toLocalDateTime(LocalDate day, LocalTime time) {
        return LocalDateTime.of(day, time);
    }

    public static LocalDate toLocalDate(String dayStr) {
        if (StringUtils.isEmpty(dayStr)) return null;
        DateTimeFormatter f = DateTimeFormatter.ofPattern(DATE_PATTERN);
        return LocalDate.parse(dayStr, f);
    }

    public static LocalTime toLocalTime(String timeStr) {
        if (StringUtils.isEmpty(timeStr)) return null;
        DateTimeFormatter f = DateTimeFormatter.ofPattern(TIME_PATTERN);
        return LocalTime.parse(timeStr, f);
    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    public static String format(LocalDateTime time) {
        if (null == time) return null;
        return DateTimeFormatter.ofPattern(DATE_TIME_PATTERN).format(time);
    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    public static String formatDay(LocalDateTime time) {
        if (null == time) return null;
        return DateTimeFormatter.ofPattern(DATE_PATTERN).format(time);
    }

    /**
     * 格式化月
     *
     * @param time
     * @return
     */
    public static String formatMonth(LocalDateTime time) {
        if (null == time) return null;
        return DateTimeFormatter.ofPattern(YEAR_MONTH_PATTERN).format(time);
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String formatDay(LocalDate date) {
        if (null == date) return null;
        return DateTimeFormatter.ofPattern(DATE_PATTERN).format(date);
    }

    /**
     * @param time
     * @return
     */
    public static String formatDayWithWeek(LocalDateTime time) {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd(EEEE)").format(time).replace("星期", "周");
    }

    /**
     * 格式化时间，小时:分钟格式
     *
     * @param time
     * @return
     */
    public static String formatTimeSecond(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(HOUR_MIN_PATTERN).format(time);
    }

    public static String formatTimeSecond(LocalTime time) {
        return DateTimeFormatter.ofPattern(HOUR_MIN_PATTERN).format(time);
    }

    public static String formatTime(LocalTime time) {
        return DateTimeFormatter.ofPattern(TIME_PATTERN).format(time);
    }

    // 构建时间区间
    public static List<String> timeRange(String stime, String etime, Integer minuteRange) {

        int[] stimes = StrUtil.splitToInt(stime, ":");
        int[] etimes = StrUtil.splitToInt(etime, ":");

        LocalTime s = LocalTime.of(stimes[0], stimes[1]);
        LocalTime e = LocalTime.of(etimes[0], etimes[1]);

        List<String> rs = new ArrayList<>();
        while (s.isBefore(e)) {
            String s1 = DateUs.formatTimeSecond(s);
            s = s.plusMinutes(minuteRange);
            String s2 = DateUs.formatTimeSecond(s);
            rs.add(s1 + "-" + s2);
        }
        return rs;
    }


    public static LocalDateTime beginOfMonth(LocalDateTime dateTime) {
        return LocalDateTime.of(
                dateTime.getYear(),
                dateTime.getMonthValue(),
                dateTime.getDayOfMonth(),
                0,
                0);

    }

}